<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta property="og:type" content="website">
<meta property="og:title" content="Frone&#39;s Blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Frone&#39;s Blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Frone&#39;s Blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/"/>





  <title>Frone's Blog</title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Frone's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术小窝</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/04/12/python3比多线程和多进程还好的新模块 —— 协程Coroutine/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/04/12/python3比多线程和多进程还好的新模块 —— 协程Coroutine/" itemprop="url">python3比多线程和多进程还好的新模块 —— 协程Coroutine</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-12T14:19:51+08:00">
                2019-04-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2019/04/12/python3比多线程和多进程还好的新模块 —— 协程Coroutine/" class="leancloud_visitors" data-flag-title="python3比多线程和多进程还好的新模块 —— 协程Coroutine">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="引子"><a href="#引子" class="headerlink" title="引子"></a>引子</h1><p>最近把所有系统的python3 版本都更新到了python3.7，然后更新了一下代码，发现这个版本改动还是很大的，之前更多还是在使用python2.7做ETL或者操作一些API，没想到python的变化如此之大，看来自己还是太落伍了。于是在知乎和官网上找资料学习了下，看到一篇讲协程的文章很受启发，以后应该会较多使用这个功能，之前使用的多进程多线程效果都不明显，而协程应该是一个python的效率利器。</p>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>多进程和多线程除了创建的开销大之外还有一个难以根治的缺陷，就是处理进程之间或线程之间的协作问题，因为是依赖多进程和多线程的程序在不加锁的情况下通常是不可控的，而协程则可以完美地解决协作问题，由用户来决定协程之间的调度。</p>
<p>总所周知，Python因为有GIL（全局解释锁）这玩意，不可能有真正的多线程的存在，因此很多情况下都会用multiprocessing实现并发，而且在Python中应用多线程还要注意关键地方的同步，不太方便，用协程代替多线程和多进程是一个很好的选择，因为它吸引人的特性：<em>主动调用/退出，状态保存，避免cpu上下文切换</em>等…</p>
<h1 id="协程"><a href="#协程" class="headerlink" title="协程"></a>协程</h1><h2 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h2><p>协程，又称作<strong>Coroutine</strong>,通过 async/await 语法进行声明，是编写异步应用的推荐方式。</p>
<p>从字面上来理解，即协同运行的例程，它是比是线程（thread）更细量级的用户态线程，特点是允许用户的主动调用和主动退出，挂起当前的例程然后返回值或去执行其他任务，接着返回原来停下的点继续执行。等下，这是否有点奇怪？我们都知道一般函数都是线性执行的，不可能说执行到一半返回，等会儿又跑到原来的地方继续执行。但一些熟悉python（or其他动态语言）的童鞋都知道这可以做到，答案是用yield语句。其实这里我们要感谢操作系统（OS）为我们做的工作，因为它具有getcontext和swapcontext这些特性，通过系统调用，我们可以把上下文和状态保存起来，切换到其他的上下文，这些特性为coroutine的实现提供了底层的基础。操作系统的Interrupts和Traps机制则为这种实现提供了可能性，因此它看起来可能是下面这样的:</p>
<p><img src="http://img.alpstudy.com//imgs/20190411141855_cS0JTt_Screenshot.jpeg" alt=""></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">import</span> asyncio</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line"><span class="meta">... </span>    print(<span class="string">'hello'</span>)</span><br><span class="line"><span class="meta">... </span>    <span class="keyword">await</span> asyncio.sleep(<span class="number">1</span>)</span><br><span class="line"><span class="meta">... </span>    print(<span class="string">'world'</span>)</span><br><span class="line"></span><br><span class="line"><span class="meta">&gt;&gt;&gt; </span>asyncio.run(main())</span><br><span class="line">hello</span><br><span class="line">world</span><br></pre></td></tr></table></figure>
<h2 id="理解生成器（generator）"><a href="#理解生成器（generator）" class="headerlink" title="理解生成器（generator）"></a>理解生成器（generator）</h2><p>学过生成器和迭代器的同学应该都知道python有yield这个关键字，yield能把一个函数变成一个generator，与return不同，yield在函数中返回值时会保存函数的状态，使下一次调用函数时会从上一次的状态继续执行，即从yield的下一条语句开始执行，这样做有许多好处，比如我们想要生成一个数列，若该数列的存储空间太大，而我们仅仅需要访问前面几个元素，那么yield就派上用场了，它实现了这种一边循环一边计算的机制，节省了存储空间，提高了运行效率。</p>
<h2 id="运行协程"><a href="#运行协程" class="headerlink" title="运行协程"></a>运行协程</h2><ol>
<li><p><a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio.run" target="_blank" rel="noopener"><code>asyncio.run()</code></a> 函数用来运行最高层级的入口点 “main()” 函数</p>
</li>
<li><p>等待一个协程。以下代码段会在等待 1 秒后打印 “hello”，然后 <em>再次</em> 等待 2 秒后打印 “world”:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"><span class="keyword">import</span> time</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">say_after</span><span class="params">(delay, what)</span>:</span></span><br><span class="line">    <span class="keyword">await</span> asyncio.sleep(delay)</span><br><span class="line">    print(what)</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    print(<span class="string">f"started at <span class="subst">&#123;time.strftime(<span class="string">'%X'</span>)&#125;</span>"</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">await</span> say_after(<span class="number">1</span>, <span class="string">'hello'</span>)</span><br><span class="line">    <span class="keyword">await</span> say_after(<span class="number">2</span>, <span class="string">'world'</span>)</span><br><span class="line"></span><br><span class="line">    print(<span class="string">f"finished at <span class="subst">&#123;time.strftime(<span class="string">'%X'</span>)&#125;</span>"</span>)</span><br><span class="line"></span><br><span class="line">asyncio.run(main())</span><br></pre></td></tr></table></figure>
</li>
</ol>
<ol>
<li><p><a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio.create_task" target="_blank" rel="noopener"><code>asyncio.create_task()</code></a> 函数用来<strong>并发</strong>运行作为 asyncio <a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio.Task" target="_blank" rel="noopener"><code>任务</code></a> 的多个协程。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    task1 = asyncio.create_task(</span><br><span class="line">        say_after(<span class="number">1</span>, <span class="string">'hello'</span>))</span><br><span class="line"></span><br><span class="line">    task2 = asyncio.create_task(</span><br><span class="line">        say_after(<span class="number">2</span>, <span class="string">'world'</span>))</span><br><span class="line"></span><br><span class="line">    print(<span class="string">f"started at <span class="subst">&#123;time.strftime(<span class="string">'%X'</span>)&#125;</span>"</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Wait until both tasks are completed (should take</span></span><br><span class="line">    <span class="comment"># around 2 seconds.)</span></span><br><span class="line">    <span class="keyword">await</span> task1</span><br><span class="line">    <span class="keyword">await</span> task2</span><br><span class="line"></span><br><span class="line">    print(<span class="string">f"finished at <span class="subst">&#123;time.strftime(<span class="string">'%X'</span>)&#125;</span>"</span>)</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h2 id="可等待对象"><a href="#可等待对象" class="headerlink" title="可等待对象"></a>可等待对象</h2><p>如果一个对象可以在 await 语句中使用，那么它就是 可等待 对象。许多 asyncio API 都被设计为接受可等待对象。</p>
<p>可等待 对象有三种主要类型: <strong>协程, 任务 和 Future</strong>.</p>
<h3 id="协程-1"><a href="#协程-1" class="headerlink" title="协程"></a>协程</h3><p>Python 协程属于 <em>可等待</em> 对象，因此可以在其他协程中被等待:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">nested</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">42</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="comment"># Nothing happens if we just call "nested()".</span></span><br><span class="line">    <span class="comment"># A coroutine object is created but not awaited,</span></span><br><span class="line">    <span class="comment"># so it *won't run at all*.</span></span><br><span class="line">    nested()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># Let's do it differently now and await it:</span></span><br><span class="line">    print(<span class="keyword">await</span> nested())  <span class="comment"># will print "42".</span></span><br><span class="line"></span><br><span class="line">asyncio.run(main())</span><br></pre></td></tr></table></figure>
<p><strong>重要</strong></p>
<p>在本文档中 “协程” 可用来表示两个紧密关联的概念:</p>
<ul>
<li><em>协程函数</em>: 定义形式为 <a href="https://docs.python.org/zh-cn/3/reference/compound_stmts.html#async-def" target="_blank" rel="noopener"><code>async def</code></a> 的函数;</li>
<li><em>协程对象</em>: 调用 <em>协程函数</em> 所返回的对象。</li>
</ul>
<p>asyncio 也支持旧式的 <a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio-generator-based-coro" target="_blank" rel="noopener">基于生成器的</a> 协程。</p>
<h3 id="任务"><a href="#任务" class="headerlink" title="任务"></a>任务</h3><p><em>任务</em> 被用来设置日程以便 <em>并发</em> 执行协程。</p>
<p>当一个协程通过 <a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio.create_task" target="_blank" rel="noopener"><code>asyncio.create_task()</code></a> 等函数被打包为一个 <em>任务</em>，该协程将自动排入日程准备立即运行:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">nested</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">42</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="comment"># Schedule nested() to run soon concurrently</span></span><br><span class="line">    <span class="comment"># with "main()".</span></span><br><span class="line">    task = asyncio.create_task(nested())</span><br><span class="line"></span><br><span class="line">    <span class="comment"># "task" can now be used to cancel "nested()", or</span></span><br><span class="line">    <span class="comment"># can simply be awaited to wait until it is complete:</span></span><br><span class="line">    <span class="keyword">await</span> task</span><br><span class="line"></span><br><span class="line">asyncio.run(main())</span><br></pre></td></tr></table></figure>
<h3 id="Future-对象"><a href="#Future-对象" class="headerlink" title="Future 对象"></a>Future 对象</h3><p><a href="https://docs.python.org/zh-cn/3/library/asyncio-future.html#asyncio.Future" target="_blank" rel="noopener"><code>Future</code></a> 是一种特殊的 <strong>低层级</strong> 可等待对象，表示一个异步操作的 <strong>最终结果</strong>。</p>
<p>当一个 Future 对象 <em>被等待</em>，这意味着协程将保持等待直到该 Future 对象在其他地方操作完毕。</p>
<p>在 asyncio 中需要 Future 对象以便允许通过 async/await 使用基于回调的代码。</p>
<p>通常情况下 <strong>没有必要</strong> 在应用层级的代码中创建 Future 对象。</p>
<p>Future 对象有时会由库和某些 asyncio API 暴露给用户，用作可等待对象:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="keyword">await</span> function_that_returns_a_future_object()</span><br><span class="line"></span><br><span class="line">    <span class="comment"># this is also valid:</span></span><br><span class="line">    <span class="keyword">await</span> asyncio.gather(</span><br><span class="line">        function_that_returns_a_future_object(),</span><br><span class="line">        some_python_coroutine()</span><br><span class="line">    )</span><br></pre></td></tr></table></figure>
<p>一个很好的返回对象的低层级函数的示例是 <a href="https://docs.python.org/zh-cn/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor" target="_blank" rel="noopener"><code>loop.run_in_executor()</code></a>。</p>
<h2 id="并发运行任务"><a href="#并发运行任务" class="headerlink" title="并发运行任务"></a>并发运行任务</h2><p><a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio.gather" target="_blank" rel="noopener"><em>awaitable</em> asyncio.gather(<em>*aws</em>, <em>loop=None</em>, <em>return_exceptions=False</em>)</a></p>
<p><em>并发</em> 运行 <em>aws</em> 序列中的 <a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html#asyncio-awaitables" target="_blank" rel="noopener">可等待对象</a>。</p>
<p>如果 <em>aws</em> 中的某个可等待对象为协程，它将自动作为一个任务加入日程。</p>
<p>如果所有可等待对象都成功完成，结果将是一个由所有返回值聚合而成的列表。结果值的顺序与 <em>aws</em> 中可等待对象的顺序一致。</p>
<p>如果 <em>return_exceptions</em> 为 <code>False</code> (默认)，所引发的首个异常会立即传播给等待 <code>gather()</code> 的任务。<em>aws</em>序列中的其他可等待对象 <strong>不会被取消</strong> 并将继续运行。</p>
<p>如果 <em>return_exceptions</em> 为 <code>True</code>，异常会和成功的结果一样处理，并聚合至结果列表。</p>
<p>如果 <code>gather()</code> <em>被取消</em>，所有被提交 (尚未完成) 的可等待对象也会 <em>被取消</em>。</p>
<p>如果 <em>aws</em> 序列中的任一 Task 或 Future 对象 <em>被取消</em>，它将被当作引发了 <a href="https://docs.python.org/zh-cn/3/library/asyncio-exceptions.html#asyncio.CancelledError" target="_blank" rel="noopener"><code>CancelledError</code></a> 一样处理 — 在此情况下 <code>gather()</code> 调用 <strong>不会</strong> 被取消。这是为了防止一个已提交的 Task/Future 被取消导致其他 Tasks/Future 也被取消。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">factorial</span><span class="params">(name, number)</span>:</span></span><br><span class="line">    f = <span class="number">1</span></span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>, number + <span class="number">1</span>):</span><br><span class="line">        print(<span class="string">f"Task <span class="subst">&#123;name&#125;</span>: Compute factorial(<span class="subst">&#123;i&#125;</span>)..."</span>)</span><br><span class="line">        <span class="keyword">await</span> asyncio.sleep(<span class="number">1</span>)</span><br><span class="line">        f *= i</span><br><span class="line">    print(<span class="string">f"Task <span class="subst">&#123;name&#125;</span>: factorial(<span class="subst">&#123;number&#125;</span>) = <span class="subst">&#123;f&#125;</span>"</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    <span class="comment"># Schedule three calls *concurrently*:</span></span><br><span class="line">    <span class="keyword">await</span> asyncio.gather(</span><br><span class="line">        factorial(<span class="string">"A"</span>, <span class="number">2</span>),</span><br><span class="line">        factorial(<span class="string">"B"</span>, <span class="number">3</span>),</span><br><span class="line">        factorial(<span class="string">"C"</span>, <span class="number">4</span>),</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">asyncio.run(main())</span><br><span class="line"></span><br><span class="line"><span class="comment"># Expected output:</span></span><br><span class="line"><span class="comment">#</span></span><br><span class="line"><span class="comment">#     Task A: Compute factorial(2)...</span></span><br><span class="line"><span class="comment">#     Task B: Compute factorial(2)...</span></span><br><span class="line"><span class="comment">#     Task C: Compute factorial(2)...</span></span><br><span class="line"><span class="comment">#     Task A: factorial(2) = 2</span></span><br><span class="line"><span class="comment">#     Task B: Compute factorial(3)...</span></span><br><span class="line"><span class="comment">#     Task C: Compute factorial(3)...</span></span><br><span class="line"><span class="comment">#     Task B: factorial(3) = 6</span></span><br><span class="line"><span class="comment">#     Task C: Compute factorial(4)...</span></span><br><span class="line"><span class="comment">#     Task C: factorial(4) = 24</span></span><br></pre></td></tr></table></figure>
<h1 id="爬虫例子"><a href="#爬虫例子" class="headerlink" title="爬虫例子"></a>爬虫例子</h1><p>使用爬虫爬取豆瓣top250</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> time</span><br><span class="line"><span class="keyword">import</span> asyncio</span><br><span class="line"><span class="keyword">import</span> aiohttp</span><br><span class="line"></span><br><span class="line">url = <span class="string">"https://movie.douban.com/top250"</span></span><br><span class="line">header = &#123;</span><br><span class="line">    <span class="string">"User-Agent"</span>: <span class="string">"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"</span>,</span><br><span class="line">    <span class="string">"content-type"</span>: <span class="string">"text/plain;charset=UTF-8"</span>,</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">fetch_content</span><span class="params">(url)</span>:</span></span><br><span class="line">    <span class="comment"># await asyncio.sleep(1) # 防止请求过快 等待1秒</span></span><br><span class="line">    <span class="keyword">async</span> <span class="keyword">with</span> aiohttp.ClientSession(</span><br><span class="line">        headers=header, connector=aiohttp.TCPConnector(ssl=<span class="keyword">False</span>)</span><br><span class="line">    ) <span class="keyword">as</span> session:</span><br><span class="line">        <span class="keyword">async</span> <span class="keyword">with</span> session.get(url) <span class="keyword">as</span> response:</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">await</span> response.text()</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">parse</span><span class="params">(url)</span>:</span></span><br><span class="line">    page = <span class="keyword">await</span> fetch_content(url)</span><br><span class="line">    html = etree.HTML(page)</span><br><span class="line"></span><br><span class="line">    xpath_movie = <span class="string">'//*[@id="content"]/div/div[1]/ol/li'</span></span><br><span class="line">    xpath_title = <span class="string">'.//span[@class="title"]'</span></span><br><span class="line">    xpath_pages = <span class="string">'//*[@id="content"]/div/div[1]/div[2]/a'</span></span><br><span class="line">    xpath_descs = <span class="string">'.//span[@class="inq"]'</span></span><br><span class="line">    xpath_links = <span class="string">'.//div[@class="info"]/div[@class="hd"]/a'</span></span><br><span class="line"></span><br><span class="line">    pages = html.xpath(xpath_pages)  <span class="comment"># 所有页面的链接都在底部获取</span></span><br><span class="line">    fetch_list = []</span><br><span class="line">    result = []</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> element_movie <span class="keyword">in</span> html.xpath(xpath_movie):</span><br><span class="line">        result.append(element_movie)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> p <span class="keyword">in</span> pages:</span><br><span class="line">        fetch_list.append(url + p.get(<span class="string">"href"</span>))  <span class="comment"># 解析翻页按钮对应的链接 组成完整后边页面链接</span></span><br><span class="line"></span><br><span class="line">    tasks = [fetch_content(url) <span class="keyword">for</span> url <span class="keyword">in</span> fetch_list]  <span class="comment"># 并行处理所有翻页的页面</span></span><br><span class="line">    pages = <span class="keyword">await</span> asyncio.gather(*tasks)</span><br><span class="line">    <span class="comment"># 并发 运行 aws 序列中的 可等待对象。</span></span><br><span class="line">    <span class="comment"># 如果 aws 中的某个可等待对象为协程，它将自动作为一个任务加入日程。</span></span><br><span class="line">    <span class="comment"># 如果所有可等待对象都成功完成，结果将是一个由所有返回值聚合而成的列表。结果值的顺序与 aws 中可等待对象的顺序一致。</span></span><br><span class="line">    <span class="keyword">for</span> page <span class="keyword">in</span> pages:</span><br><span class="line">        html = etree.HTML(page)</span><br><span class="line">        <span class="keyword">for</span> element_movie <span class="keyword">in</span> html.xpath(xpath_movie):</span><br><span class="line">            result.append(element_movie)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> i, movie <span class="keyword">in</span> enumerate(result, <span class="number">1</span>):</span><br><span class="line">        title = movie.find(xpath_title).text</span><br><span class="line">        desc = (</span><br><span class="line">            <span class="string">"&lt;"</span> + movie.find(xpath_descs).text + <span class="string">"&gt;"</span></span><br><span class="line">            <span class="keyword">if</span> movie.find(xpath_descs) <span class="keyword">is</span> <span class="keyword">not</span> <span class="keyword">None</span></span><br><span class="line">            <span class="keyword">else</span> <span class="keyword">None</span></span><br><span class="line">        )</span><br><span class="line">        link = movie.find(xpath_links).get(<span class="string">"href"</span>)</span><br><span class="line">        print(i, title, desc, link)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">async</span> <span class="function"><span class="keyword">def</span> <span class="title">main</span><span class="params">()</span>:</span></span><br><span class="line">    start = time()</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">5</span>):</span><br><span class="line">        <span class="keyword">await</span> parse(url)</span><br><span class="line">    end = time()</span><br><span class="line">    print(<span class="string">"Cost &#123;&#125; seconds"</span>.format((end - start) / <span class="number">5</span>))</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:</span><br><span class="line">    asyncio.run(main())</span><br></pre></td></tr></table></figure>
<h1 id="参考文章"><a href="#参考文章" class="headerlink" title="参考文章"></a>参考文章</h1><ol>
<li><a href="https://zhuanlan.zhihu.com/p/25228075" target="_blank" rel="noopener">从0到1，Python异步编程的演进之路</a></li>
<li><a href="https://www.jianshu.com/p/5d38b598d8a6" target="_blank" rel="noopener">Python3 Async/Await解释</a></li>
<li><a href="https://docs.python.org/zh-cn/3/library/asyncio-task.html" target="_blank" rel="noopener">官方文档 协程与任务</a></li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/04/12/阿兹卡班安装介绍/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/04/12/阿兹卡班安装介绍/" itemprop="url">阿兹卡班安装及介绍</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-12T13:19:51+08:00">
                2019-04-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/调度/" itemprop="url" rel="index">
                    <span itemprop="name">调度</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2019/04/12/阿兹卡班安装介绍/" class="leancloud_visitors" data-flag-title="阿兹卡班安装及介绍">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>Azkaban是一种类似于Oozie的工作流控制引擎，可以用来解决多个Hadoop（或Spark等）离线计算任务之间的依赖关系问题。</p>
<p>也可以用其代替cron来对周期性任务进行调度，并且更为直观，可靠，同时提供了美观的可视化管理界面。</p>
<p>下文将对azkaban对spark离线任务调度进行简要说明。</p>
<h2 id="azkaban由三部分构成"><a href="#azkaban由三部分构成" class="headerlink" title="azkaban由三部分构成"></a>azkaban由三部分构成</h2><ul>
<li>Relational Database(Mysql)</li>
<li>Azkaban Web Server</li>
<li>Azkaban Executor Server</li>
</ul>
<h3 id="Relational-Database-Mysql"><a href="#Relational-Database-Mysql" class="headerlink" title="Relational Database(Mysql)"></a>Relational Database(Mysql)</h3><p>azkaban将大多数状态信息都存于Mysql中,Azkaban Web Server 和 Azkaban Executor Server也需要访问DB。</p>
<h3 id="Azkaban-Web-Server"><a href="#Azkaban-Web-Server" class="headerlink" title="Azkaban Web Server"></a>Azkaban Web Server</h3><p>提供了Web UI，是azkaband的主要管理者，包括 project 的管理，认证，调度，对工作流执行过程的监控等。</p>
<h3 id="Azkaban-Executor-Server"><a href="#Azkaban-Executor-Server" class="headerlink" title="Azkaban Executor Server"></a>Azkaban Executor Server</h3><p>调度工作流和任务，纪录工作流活任务的日志，之所以将AzkabanWebServer和AzkabanExecutorServer分开，主要是因为在某个任务流失败后，可以更方便的将重新执行。而且也更有利于Azkaban系统的升级。</p>
<h3 id="可调度任务类型"><a href="#可调度任务类型" class="headerlink" title="可调度任务类型"></a>可调度任务类型</h3><ul>
<li>linux命令</li>
<li>脚本</li>
<li>java程序</li>
<li>hadoop MR</li>
<li>hive</li>
</ul>
<h1 id="创建任务流"><a href="#创建任务流" class="headerlink" title="创建任务流"></a>创建任务流</h1><h2 id="创建任务"><a href="#创建任务" class="headerlink" title="创建任务"></a>创建任务</h2><p>只需要创建一个.job结尾的文本文件，通过dependencies字段指定依赖的上层执行脚本</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># foo.job</span><br><span class="line">type=command</span><br><span class="line">command=echo &quot;Hello World&quot;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># bar.job</span><br><span class="line">type=command</span><br><span class="line">dependencies=foo</span><br><span class="line">command=echo bar</span><br></pre></td></tr></table></figure>
<h2 id="上传任务流"><a href="#上传任务流" class="headerlink" title="上传任务流"></a>上传任务流</h2><p>将所有job文件打包成一个zip文件上传即可</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">zip -q foo.job.zip foo.job</span><br></pre></td></tr></table></figure>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ol>
<li><a href="https://blog.csdn.net/lsshlsw/article/details/50831239" target="_blank" rel="noopener">使用Azkaban调度Spark任务</a></li>
<li><a href="http://zeyulee.com/bigdata/2017/09/06/azkaban3-installation-and-configuration/" target="_blank" rel="noopener">Azkaban 3的安装与配置</a></li>
<li><a href="https://www.jdon.com/workflow/Airflow-vs-Azkaban-vs-Conductor-vs-Oozie-vs-Amazon-Step-Functions.html" target="_blank" rel="noopener">2018工作流引擎比较：Airflow、Azkaban、Conductor、Oozie和 Amazon Step Functions</a></li>
<li><a href="https://medium.com/@xunnan.xu/workflow-processing-engine-overview-2018-airflow-vs-azkaban-vs-conductor-vs-oozie-vs-amazon-step-90affc54d53b" target="_blank" rel="noopener">英文版</a></li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/04/12/阿里云ECS安装mysql/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/04/12/阿里云ECS安装mysql/" itemprop="url">阿里云ESC安装mysql</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-04-12T12:19:51+08:00">
                2019-04-12
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/阿里云/" itemprop="url" rel="index">
                    <span itemprop="name">阿里云</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2019/04/12/阿里云ECS安装mysql/" class="leancloud_visitors" data-flag-title="阿里云ESC安装mysql">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">lsb_release -a # 查看系统版本</span><br><span class="line"></span><br><span class="line">yum -y update # 更新系统软件</span><br><span class="line"></span><br><span class="line">wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm # 下载rpm包</span><br></pre></td></tr></table></figure>
<h1 id="开始安装"><a href="#开始安装" class="headerlink" title="开始安装"></a>开始安装</h1><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">rpm -Uvh mysql80-community-release-el7-2.noarch.rpm</span><br><span class="line"></span><br><span class="line">yum repolist all | grep mysql # 查看安装mysql配置文件</span><br><span class="line"></span><br><span class="line">sudo yum install mysql-community-server</span><br></pre></td></tr></table></figure>
<h1 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">service mysqld start</span><br><span class="line"></span><br><span class="line">service mysqld status</span><br><span class="line"></span><br><span class="line">ps -aux|grep mysql # 查看进程</span><br><span class="line">netstat -anp|grep `pid` # 查看进程对应端口</span><br><span class="line"></span><br><span class="line"># 通过端口查看进程</span><br><span class="line">netstat -nap | grep 端口号</span><br></pre></td></tr></table></figure>
<p>Mysql服务启动后发生如下事件</p>
<ol>
<li><p>mysql server初始化</p>
</li>
<li><p>An SSL certificate and key files are generated in the data directory.</p>
</li>
<li><p>The <a href="https://dev.mysql.com/doc/refman/8.0/en/validate-password.html" target="_blank" rel="noopener">validate_password plugin</a> is installed and enabled.</p>
</li>
<li><p>A superuser account <code>&#39;root&#39;@&#39;localhost&#39;</code> is created. A password for the superuser is set and stored in the error log file. To reveal it, use the following command:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">grep 'temporary password' /var/log/mysqld.log</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> 可以使用如下方法更新密码</span><br><span class="line"><span class="meta">shell&gt;</span> mysql -uroot -p'*****'</span><br><span class="line"><span class="meta">mysql&gt;</span> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';</span><br><span class="line"><span class="meta">mysql&gt;</span> update user set Host = '%' where user ='root';  # 开启远程登录</span><br><span class="line"><span class="meta">mysql&gt;</span> flush privileges;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h1 id="安全相关设置"><a href="#安全相关设置" class="headerlink" title="安全相关设置"></a>安全相关设置</h1><ol>
<li>在ECS管理页面添加安全组规则</li>
</ol>
<p><img src="http://img.alpstudy.com//imgs/20190402183134_HmTblW_Screenshot.jpeg" alt=""></p>
<ol>
<li>添加3306 入口规则，无需启用防火墙</li>
</ol>
<p><img src="/Users/xingshulin/Library/Application Support/typora-user-images/image-20190402183207794.png" alt="image-20190402183207794"></p>
<h1 id=""><a href="#" class="headerlink" title=" "></a> </h1><h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ol>
<li><a href="https://blog.csdn.net/panyox/article/details/77780098" target="_blank" rel="noopener">阿里云ECS服务器自建数据库的一些坑</a></li>
<li><p><a href="https://yq.aliyun.com/articles/285398" target="_blank" rel="noopener">阿里云ECS服务器CentOS7上安装MySql服务</a></p>
</li>
<li><p><a href="https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/" target="_blank" rel="noopener">https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/</a></p>
</li>
<li><a href="https://www.cnblogs.com/chanshuyi/p/mysql_user_mng.html" target="_blank" rel="noopener">用户管理</a></li>
</ol>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/02/02/2018年度总结/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2019/02/02/2018年度总结/" itemprop="url">2018年度总结</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-02-02T15:19:51+08:00">
                2019-02-02
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/随笔/" itemprop="url" rel="index">
                    <span itemprop="name">随笔</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2019/02/02/2018年度总结/" class="leancloud_visitors" data-flag-title="2018年度总结">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><blockquote>
<p>当你老了，回顾一生，就会发觉：什么时候出国读书，什么时候决定做第一份职业、何时选定了对象而恋爱、什么时候结婚，其实都是命运的巨变。只是当时站在三岔路口，眼见风云千樯，你作出选择的那一日，在日记上，相当沉闷和平凡，当时还以为是生命中普通的一天。</p>
<p>——<a href="https://www.juzimi.com/writer/陶杰" target="_blank" rel="noopener">陶杰</a> 《<a href="https://www.juzimi.com/article/杀鹌鹑的少女" target="_blank" rel="noopener">杀鹌鹑的少女</a>》</p>
</blockquote>
<p>在2019年到来的时候，看到了这么一句话，感触颇深，生命就死这么组成的，开始的时候我们太年轻，意识不到，就要依赖父母的帮助。但是如果父母帮不上忙呢，那就真的看命了，年轻的固执如果没有本钱那就是作死吧。</p>
<h1 id="读书"><a href="#读书" class="headerlink" title="读书"></a>读书</h1><p>2018年做的比较好的就是读书了，阅读量显著提升了很多，也买了不少书，有纸质书（技术类偏多，还有少量管理书籍和休闲书籍）。今年发现中信出版社出版的书籍质量还是很高的，当然还是专注在管理方面，虽然工作中用到的不是很多，但是个人提升还是作用不小。</p>
<p>读书看电影还是用豆瓣记录比较合适，豆瓣评分还是比较客观的，而且书很全，也是对自己监督的一个工具。</p>
<p><img src="http://img.alpstudy.com/blog/20190108/Ub7e9igvxyc1.png?imageslim" alt="mark"></p>
<p><img src="http://img.alpstudy.com/blog/20190108/6l3jWDqK5GJu.png?imageslim" alt="mark"></p>
<p>今年看完的没看完的书应该不止这些，微信读书应该是最多的，在双十一的时候还买了季卡，看《沙海》电视剧的时候读了一段时间《盗墓笔记》，应该看了三本。然后在看了央视的《一本好书》之后看完了《三体》，这是我相当喜欢的一套书，好像找回了小时候看凡尔纳科幻小说的感觉，我还看了大刘的中篇科幻集《时间移民》，然后完结了一直没看完的《1Q84 BOOK3》,以上是小说类的。</p>
<p>在工作方面受亮叔影响看了很多数据类的书籍，的确深入学习是我比较需要的，主要在数据仓库和数据驱动这两块，既要有技术底层，又要有理论能力，会干能说才吃香。看完了《数据的本质》，《用数据讲故事》，在看《数据仓库工具箱》，《大数据之路》。</p>
<p>管理类的书籍看了《精力管理》《格鲁夫给经理人的第一课》《稀缺》还买了很多没有看，要加油消化掉。</p>
<h1 id="电影"><a href="#电影" class="headerlink" title="电影"></a>电影</h1><p>电影就不一一赘述了，院线看了些有的没的，用新买的玩客云下了不少没上映的和不上映的，还是对惊悚悬疑类的比较感冒，温子仁是个不错的导演，那么经典的《saw》也是他导演的啊。。。</p>
<p>印象比较深刻的电影有《海王》《此房是我造》《升级》</p>
<h1 id="技术学习"><a href="#技术学习" class="headerlink" title="技术学习"></a>技术学习</h1><p>在技术方面专注在了数仓和阿里云平台上，重点使用了maxcompute和dataworks两款产品。</p>
<p>技术上收获的更多是广度吧，流程和目的更加清晰了，知道怎么样能够叫高效、低成本的完成一件事情。</p>
<h1 id="生活"><a href="#生活" class="headerlink" title="生活"></a>生活</h1><p>生活应该是改变最多的地方，2018年举办了婚礼，完成了人生的重要一步。不过生活和自己预计的差了很多，的确家庭环境影响很大。一直把自己家定位在平均水平左右，看来我错了，中国的平均水平低是因为中国有广大的贫困人口，农民、山区的人年收入可能就万把块。要是把自己定位在平均水平就觉得OK，那就是真的不能OK了。</p>
<p>结婚之后认识到了很多问题，结婚的时候和三叔聊过之后就更清晰了，我的父母和我弟弟的父母都是失败的父母，典型的草根失败人民，没赚到什么钱，对家庭也没什么帮助，这应该就是草根中的草根了吧，我最难受的一点就是跟父母没法深入沟通，真的很痛苦、无助，他们完全理解不了现在这个时代，也理解不了你。</p>
<p>可能抱怨有点多了吧，我能做的就是接受吧，还有沉默。或许更应该想清楚自己要成为什么样的人，过怎么样的一生，比生儿育女、家庭美满更重要，失败的人生又怎么能孕育幸福的家庭呢，不该说的不敢去说，和谐二字涵盖了太多，那就活着吧。</p>
<h1 id="你好-2019"><a href="#你好-2019" class="headerlink" title="你好 2019"></a>你好 2019</h1><p>2019我34了，不确定性在互联网这个行业里变多了，需要做一些真正自己应该做的事情了。不忘初心，做回自己吧，希望这一年过的开心。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/30/airflow EmailOperator 发送邮件问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/08/30/airflow EmailOperator 发送邮件问题/" itemprop="url">airflow EmailOperator 发送邮件 附件文件名丢失或乱码问题</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-30T08:21:51+08:00">
                2018-08-30
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/任务调度/" itemprop="url" rel="index">
                    <span itemprop="name">任务调度</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/30/airflow EmailOperator 发送邮件问题/" class="leancloud_visitors" data-flag-title="airflow EmailOperator 发送邮件 附件文件名丢失或乱码问题">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="开始使用airflow"><a href="#开始使用airflow" class="headerlink" title="开始使用airflow"></a>开始使用airflow</h3><p>最早开始使用airflow是因为公司的服务过度 依赖crontab了，完全通过时间进行任务调度，而且不便于追踪任务运行情况，也不好处理任务之间的依赖关系。更不要说管理多服务器的crontab问题了。</p>
<p>于是自己进行软件选型，查阅相关资料。因为本身是做数据工作的，不希望只是一个简简单单的crontab功能升级，于是选择了airflow。</p>
<p>airflow的主要特点如下：</p>
<ul>
<li>我们可以将一些常做的巡检任务，定时脚本（如 crontab ），ETL处理，监控等任务放在 airflow 上集中管理，甚至都不用再写监控脚本，作业出错会自动发送日志到指定人员邮箱，低成本高效率地解决生产问题。</li>
<li>Airflow 适用于调度作业较为复杂，特别是各作业之间的依赖关系复杂的情况​</li>
</ul>
<p>备选是cronsun</p>
<ul>
<li>cronsun 是一个分布式任务系统，单个结点和 *nix 机器上的 crontab 近似。支持界面管理机器上的任务，支持任务失败邮件提醒，安装简单，使用方便，是替换 crontab 一个不错的选择）</li>
</ul>
<p>具体关于任务调度可以参考之前转发的一篇文章，<a href="https://blog.csdn.net/frone/article/details/82218370" target="_blank" rel="noopener">浅谈工作流调度系统</a></p>
<h3 id="使用-EmailOperator-模块发送邮件"><a href="#使用-EmailOperator-模块发送邮件" class="headerlink" title="使用 EmailOperator 模块发送邮件"></a>使用 EmailOperator 模块发送邮件</h3><p>当然写本篇文章的目的是解决使用airflow时遇到的发送邮件问题，我在使用EmailOperator发送xlsx附件的时候遇到了附件文件名丢失问题，有的客户端可以正常接收（例如mac的邮件），但是服务器端和Foxmail则遇到了问题，因为之前使用smtp发送邮件的时候遇到过类似问题，所以决定去airflow的源代码中一探究竟。<br><img src="https://img-blog.csdn.net/20180830114431733?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zyb25l/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="附件文件名丢失"></p>
<h3 id="修改源代码处理文件名问题"><a href="#修改源代码处理文件名问题" class="headerlink" title="修改源代码处理文件名问题"></a>修改源代码处理文件名问题</h3><p>查看源代码追踪到了问题，EmailOperator使用了 from airflow.utils.email import send_email，通过email这个类完成邮件的最终发送，所以讲问题定位到了这里。<br>第84行<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">part[&apos;Content-Disposition&apos;] = &apos;attachment; filename=&quot;%s&quot;&apos; % basename</span><br><span class="line">part[&apos;Content-ID&apos;] = &apos;&lt;%s&gt;&apos; % basename</span><br></pre></td></tr></table></figure></p>
<p>修改为<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"># update by frone due to email attachment reason</span><br><span class="line">part.add_header(&apos;Content-Disposition&apos;, &apos;attachment&apos;, filename=(&apos;utf-8&apos;, &apos;&apos;, basename))</span><br><span class="line">part.add_header(&apos;Content-ID&apos;,&apos;&lt;%s&gt;&apos;%basename)</span><br></pre></td></tr></table></figure></p>
<p>最终成功解决问题，可以正常使用airflow的邮件功能了。<br><img src="https://img-blog.csdn.net/20180830114520776?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2Zyb25l/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70" alt="问题得到解决"></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/06/设置分区问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/08/06/设置分区问题/" itemprop="url">集合分区问题</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-06T12:23:51+08:00">
                2018-08-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/06/设置分区问题/" class="leancloud_visitors" data-flag-title="集合分区问题">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <blockquote>
<p><em>本文根据PuLP文档翻译而来，原文请参考</em><br><em><a href="https://pythonhosted.org/PuLP/CaseStudies/a_set_partitioning_problem.html" target="_blank" rel="noopener">https://pythonhosted.org/PuLP/CaseStudies/a_set_partitioning_problem.html</a></em></p>
</blockquote>
<h3 id="集合分区问题"><a href="#集合分区问题" class="headerlink" title="集合分区问题"></a>集合分区问题</h3><hr>
<p>集合分区问题确定如何将一个集合（S）中的项目划分为更小的子集。S中的所有项目必须包含在一个且仅包含一个分区中。相关问题是：</p>
<ul>
<li>集合分包- 所有项目必须包含在零个或一个分区中;</li>
<li>集合覆盖 - 所有项目必须至少包含在一个分区中。</li>
</ul>
<p>在这个案例研究中，婚礼策划师必须确定婚礼的客人座位分配。为了模拟这个问题，桌子被建模为分区，邀请参加婚礼的嘉宾被建模为S的元素。婚礼策划者希望最大化所有桌子的总幸福感。<br><img src="https://pythonhosted.org/PuLP/_images/wedding_seating.jpg" alt="这里写图片描述"></p>
<p>可以通过显式枚举每个可能的子集来建模集合划分问题。虽然这种方法确实变得难以处理大量项目（不使用列生成），但它确实具有以下优点：分区的目标函数系数可以是非线性表达式（如幸福），并且仍然可以解决此问题使用线性编程。</p>
<p>首先，我们使用<a href="https://pythonhosted.org/PuLP/pulp.html#pulp.allcombinations" target="_blank" rel="noopener">allcombinations()</a>生成所有可能的桌座位列表。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#create list of all possible tables</span><br><span class="line">possible_tables = [tuple(c) for c in pulp.allcombinations(guests, max_table_size)]</span><br></pre></td></tr></table></figure></p>
<p>然后我们创建一个二进制变量，如果表将在解决方案中将为1，否则为零。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#create a binary variable to state that a table setting is used</span><br><span class="line">x = pulp.LpVariable.dicts(&apos;table&apos;, possible_tables, </span><br><span class="line">                            lowBound = 0,</span><br><span class="line">                            upBound = 1,</span><br><span class="line">                            cat = pulp.LpInteger)</span><br></pre></td></tr></table></figure></p>
<p>我们创造LpProblem然后制作目标函数。请注意，此脚本中使用的幸福函数很难以任何其他方式进行建模。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">seating_model = pulp.LpProblem(&quot;Wedding Seating Model&quot;, pulp.LpMinimize)</span><br><span class="line"></span><br><span class="line">seating_model += sum([happiness(table) * x[table] for table in possible_tables])</span><br></pre></td></tr></table></figure></p>
<p>我们指定解决方案中允许的表总数<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#specify the maximum number of tables</span><br><span class="line">seating_model += sum([x[table] for table in possible_tables]) &lt;= max_tables, \</span><br></pre></td></tr></table></figure></p>
<p>这组约束通过保证将guest虚拟机分配给一个表来定义集合分区问题。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#A guest must seated at one and only one table</span><br><span class="line">for guest in guests:</span><br><span class="line">    seating_model += sum([x[table] for table in possible_tables</span><br><span class="line">                                if guest in table]) == 1, &quot;Must_seat_%s&quot;%guest</span><br></pre></td></tr></table></figure></p>
<p>完整的文件可以在这里找到<a href="https://projects.coin-or.org/PuLP/browser/trunk/examples/wedding.py?format=txt" target="_blank" rel="noopener">wedding.py</a><br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line">&quot;&quot;&quot;</span><br><span class="line">A set partitioning model of a wedding seating problem</span><br><span class="line"></span><br><span class="line">Authors: Stuart Mitchell 2009</span><br><span class="line">&quot;&quot;&quot;</span><br><span class="line"></span><br><span class="line">import pulp</span><br><span class="line"></span><br><span class="line">max_tables = 5</span><br><span class="line">max_table_size = 4</span><br><span class="line">guests = &apos;A B C D E F G I J K L M N O P Q R&apos;.split()</span><br><span class="line"></span><br><span class="line">def happiness(table):</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    Find the happiness of the table</span><br><span class="line">    - by calculating the maximum distance between the letters</span><br><span class="line">    &quot;&quot;&quot;</span><br><span class="line">    return abs(ord(table[0]) - ord(table[-1]))</span><br><span class="line">                </span><br><span class="line">#create list of all possible tables</span><br><span class="line">possible_tables = [tuple(c) for c in pulp.allcombinations(guests, </span><br><span class="line">                                        max_table_size)]</span><br><span class="line"></span><br><span class="line">#create a binary variable to state that a table setting is used</span><br><span class="line">x = pulp.LpVariable.dicts(&apos;table&apos;, possible_tables, </span><br><span class="line">                            lowBound = 0,</span><br><span class="line">                            upBound = 1,</span><br><span class="line">                            cat = pulp.LpInteger)</span><br><span class="line"></span><br><span class="line">seating_model = pulp.LpProblem(&quot;Wedding Seating Model&quot;, pulp.LpMinimize)</span><br><span class="line"></span><br><span class="line">seating_model += sum([happiness(table) * x[table] for table in possible_tables])</span><br><span class="line"></span><br><span class="line">#specify the maximum number of tables</span><br><span class="line">seating_model += sum([x[table] for table in possible_tables]) &lt;= max_tables, \</span><br><span class="line">                            &quot;Maximum_number_of_tables&quot;</span><br><span class="line"></span><br><span class="line">#A guest must seated at one and only one table</span><br><span class="line">for guest in guests:</span><br><span class="line">    seating_model += sum([x[table] for table in possible_tables</span><br><span class="line">                                if guest in table]) == 1, &quot;Must_seat_%s&quot;%guest</span><br><span class="line"></span><br><span class="line">seating_model.solve()</span><br><span class="line"></span><br><span class="line">print(&quot;The choosen tables are out of a total of %s:&quot;%len(possible_tables))</span><br><span class="line">for table in possible_tables:</span><br><span class="line">    if x[table].value() == 1.0:</span><br><span class="line">        print(table)</span><br></pre></td></tr></table></figure></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/03/pulp线性优化3/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/08/03/pulp线性优化3/" itemprop="url">PulP线性优化（三）python编码</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-03T18:23:51+08:00">
                2018-08-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/03/pulp线性优化3/" class="leancloud_visitors" data-flag-title="PulP线性优化（三）python编码">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><em>本文根据PuLP文档翻译而来，原文请参考</em><br><em><a href="https://pythonhosted.org/PuLP/main/basic_python_coding.html" target="_blank" rel="noopener">https://pythonhosted.org/PuLP/main/basic_python_coding.html</a></em></p>
<h3 id="基本的Python编码"><a href="#基本的Python编码" class="headerlink" title="基本的Python编码"></a>基本的Python编码</h3><hr>
<p>在本课程中，您将学习Python中的基本编程，但也可以在Internet上免费获得优秀的Python语言参考资料。您可以下载<a href="http://www.diveintopython.org/" target="_blank" rel="noopener">Dive Into Python</a>这本书， 或者 在Python网站上有许多Python <a href="http://wiki.python.org/moin/BeginnersGuide" target="_blank" rel="noopener">初学者指南</a>。点击以下链接：</p>
<ul>
<li><a href="http://wiki.python.org/moin/BeginnersGuide/NonProgrammers" target="_blank" rel="noopener">BeginnersGuide /非程序员</a></li>
<li><a href="http://wiki.python.org/moin/BeginnersGuide/Programmers" target="_blank" rel="noopener">BeginnersGuide /程序员</a></li>
</ul>
<p>取决于您当前的编程知识水平。下面的代码部分假定了基本编程原理的知识，并主要关注特定于Python编程的语法。</p>
<p>注意：&gt;&gt;&gt;表示Python命令行提示符。</p>
<h3 id="Python中的循环"><a href="#Python中的循环" class="headerlink" title="Python中的循环"></a>Python中的循环</h3><hr>
<h4 id="for循环"><a href="#for循环" class="headerlink" title="for循环"></a>for循环</h4><p>一般格式是：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">为 变量 在 序列：</span><br><span class="line">    #some命令</span><br><span class="line">#other for循环之后的命令</span><br></pre></td></tr></table></figure></p>
<p>请注意，格式（缩进和新行）控制for循环的结束，而循环的开头是冒号：。</p>
<p>观察下面的循环，这类似于您将在课程中使用的循环。变量i通过字符串列表依次变为每个字符串。顶部是.py文件中的代码，底部显示输出<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">＃以下代码演示了一个列表，其中包含字符串</span><br><span class="line">ingredientslist  =  [ “Rice” ，“Water” ，“Jelly” ] </span><br><span class="line">for  i  in  ingredientslist ：</span><br><span class="line">    print  i </span><br><span class="line">print  “不再在循环中”</span><br></pre></td></tr></table></figure></p>
<p>输出<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Rice</span><br><span class="line">Water</span><br><span class="line">Jelly</span><br><span class="line">No longer in the loop</span><br></pre></td></tr></table></figure></p>
<h4 id="while循环"><a href="#while循环" class="headerlink" title="while循环"></a>while循环</h4><p>这些类似于for循环，除了它们继续循环，直到指定的条件不再为真。没有告诉while循环通过任何特定的序列。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">i = 3</span><br><span class="line">while i &lt;= 15:</span><br><span class="line">    # some commands</span><br><span class="line">    i = i + 1 # a command that will eventually end the loop is naturally</span><br><span class="line">    required</span><br><span class="line"># other commands after while loop</span><br></pre></td></tr></table></figure></p>
<p>对于这个特定的简单while循环，最好做一个for循环，但它演示了语法。如果循环之前的迭代次数需要结束，while循环是有用的，是未知的。</p>
<h4 id="if语句"><a href="#if语句" class="headerlink" title="if语句"></a>if语句</h4><p>这与上面的循环非常相似。键标识符是冒号：启动语句和缩进结束以结束它。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">if j in testlist:</span><br><span class="line">    # some commands</span><br><span class="line">elif j == 5:</span><br><span class="line">    # some commands</span><br><span class="line">else:</span><br><span class="line">    # some commands</span><br></pre></td></tr></table></figure>
<p>这里显示“elif”（else if）和“else”也可以在if语句之后使用。事实上，“else”可以在两个循环之后以相同的方式使用。</p>
<h3 id="python中的数组类型"><a href="#python中的数组类型" class="headerlink" title="python中的数组类型"></a>python中的数组类型</h3><hr>
<h4 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h4><p>列表只是一组变量组合在一起。范围函数通常用于创建整数列表，具有范围的一般格式（开始，停止，步骤）。start的默认值为0，步骤的默认值为1。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; range(3,8)</span><br><span class="line">[3,4,5,6,7]</span><br></pre></td></tr></table></figure>
<p>这是一个列表/序列。除了整数之外，列表中还可以包含字符串或整数，浮点数和字符串。它们可以通过循环（如下一节所示）或通过显式创建（如下所示）创建。请注意，print语句将向用户显示字符串/变量/ list / ….<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; a = [5,8,&quot;pt&quot;]</span><br><span class="line">&gt;&gt;&gt; print a</span><br><span class="line">[5,8,&apos;pt&apos;]</span><br><span class="line">&gt;&gt;&gt; print a[0]</span><br><span class="line">5</span><br></pre></td></tr></table></figure></p>
<h4 id="元组"><a href="#元组" class="headerlink" title="元组"></a>元组</h4><p>元组与列表基本相同，但重要的区别在于它们一旦创建就无法修改。它们由以下人员分配：</p>
<blockquote>
<blockquote>
<blockquote>
<p>X  =  （4 ，1 ，8 ，“字符串” ，[ 1 ，0 ]，（“J” ，4 ，“○” ），14 ）<br>元组可以在其中包含任何类型的数字，字符串，列表，其他元组，函数和对象。另请注意，元组中的第一个元素编号为元素“零”。访问此数据的方法是：</p>
<p>x [ 0 ]<br>4<br>x [ 3 ]<br>“string”</p>
<h4 id="字典"><a href="#字典" class="headerlink" title="字典"></a>字典</h4><p>字典是每个具有关联数据的引用键列表，其中该顺序根本不影响字典的操作。对于字典，键不是连续的整数（与列表不同），而是可以是整数，浮点数或字符串。这将变得清晰：</p>
<p>x  =  {}  ＃创建一个新的空字典 - 注意表示创建字典的大括号<br>x [ 4 ]  =  “编程”  ＃字符串“programming”被添加到字典x中，“ 4“因为它是参考<br>x [ ”游戏“ ]  =  12<br>打印 x [ ”游戏“ ]<br>12<br>在字典中，引用键和存储的值可以是任何类型的输入。新词典元素在创建时添加（使用列表，您无法访问或写入列表中超出最初定义的列表维度的位置）。</p>
</blockquote>
</blockquote>
</blockquote>
<p>成本 =  { “鸡肉” ： 1.3 ， “牛肉” ： 0.8 ， “羊肉” ： 12 }<br>打印 “肉类的成本”<br>为 我 在 成本：<br>    打印 我<br>    打印 成本[ 我]<br>成本[ “LAMB” ]  =  5<br>打印 “更新肉类成本“<br>对于 我 的 成本：<br>    打印 我的<br>    打印 成本[ i ]<br>给</p>
<p>成本 的 肉类<br>鸡肉<br>1.3<br>羊肉<br>12和<br>牛肉<br>0.8<br>更新 成本 的 肉类<br>LAMB<br>5<br>鸡<br>1.3<br>羊肉<br>12<br>牛肉<br>0.8<br>在上面的示例中，使用大括号和冒号创建字典以表示将数据分配给字典键。变量i依次分配给每个键（与列表中的变量相同）</p>
<blockquote>
<blockquote>
<blockquote>
<p>用于 我 在 范围（1 ，10 ）<br>）。然后使用此键调用字典，并返回存储在该键名下的数据。使用词典的这些类型的for循环与使用PuLP在本课程中对LP进行建模高度相关。</p>
</blockquote>
</blockquote>
</blockquote>
<h3 id="List-Tuple-Dictionary语法"><a href="#List-Tuple-Dictionary语法" class="headerlink" title="List / Tuple / Dictionary语法"></a>List / Tuple / Dictionary语法</h3><p>注意创建一个：</p>
<p>列表用方括号[];<br>元组用圆括号和逗号（，）完成;<br>字典是用括号{}完成的。<br>但是，在创建之后，当访问list / tuple / dictionary中的元素时，操作总是用方括号执行（即a [3]？）。如果a是列表或元组，则返回第四个元素。如果a是字典，它将返回使用引用键3存储的数据。</p>
<h3 id="列表生成式"><a href="#列表生成式" class="headerlink" title="列表生成式"></a>列表生成式</h3><p>Python支持List Comprehensions，这是一种快速而简洁的方法，可以在不使用多行的情况下创建列表。在简单的情况下，它们很容易理解，并且您将在本课程的代码中使用它们。</p>
<blockquote>
<blockquote>
<blockquote>
<p>a  =  [ i  for  i  in  range （5 ）]<br>a<br>[0,1,2,3,4]<br>上面的语句将创建列表[0,1,2,3,4]并将其分配给变量“a”。</p>
<p>赔率 =  [ i  for  i  in  range （25 ） if  i ％2 == 1 ]<br>赔率<br>[1,3,5,7,9,11,13,15,17,19,21,23 ]<br>上面的语句使用if语句和模数运算符（％），因此列表中只包含奇数：[1,3,5，…，19,21,23]。（注意：模数运算符从整数除法计算余数。）</p>
<p>fifths = [i for i in range(25) if i%5==0]<br>fifths<br>[0, 5, 10, 15, 20]<br>This will create a list with every fifth value in it [0,5,10,15,20]. Existing lists can also be used in the creation of new lists below:</p>
<p>a = [i for i in range(25) if (i in odds and i not in fifths)]<br>Note that this could also have been done in one step from scratch:</p>
<p>a = [i for i in range(25) if (i%2==1 and i%5==0)]<br>For a challenge you can try creating</p>
</blockquote>
</blockquote>
</blockquote>
<p>a list of prime numbers up to 100, and<br>a list of all “perfect” numbers.<br>More List Comprehensions Examples</p>
<p>Wikipedia: Perfect Numbers.</p>
<h3 id="Other-important-language-features"><a href="#Other-important-language-features" class="headerlink" title="Other important language features"></a>Other important language features</h3><hr>
<h4 id="Commenting-in-Python"><a href="#Commenting-in-Python" class="headerlink" title="Commenting in Python"></a>Commenting in Python</h4><p>使用“”“开始并结束评论部分，在文件顶部进行评论。整个代码中的注释是使用行开头的hash＃符号完成的。</p>
<h4 id="导入语句"><a href="#导入语句" class="headerlink" title="导入语句"></a>导入语句</h4><p>在您打算使用PuLP进行建模的所有Python编码的顶部，您将需要import语句。此语句使您当前正在编写的模块中的另一个模块（程序代码文件）的内容可用，即您将需要调用的pulp.py中定义的函数和值可用。在本课程中，您将使用：</p>
<blockquote>
<blockquote>
<blockquote>
<p>来自 纸浆 进口 *<br>星号表示您正在从纸浆模块中导入所有名称。现在可以调用在pulp.py中定义的函数，就好像它们是在您自己的模块中定义的那样。</p>
</blockquote>
</blockquote>
</blockquote>
<h4 id="functions"><a href="#functions" class="headerlink" title="functions"></a>functions</h4><p>Python中的函数定义如下：（def是define的缩写）</p>
<p>高清 名（inputparameter1 ， inputparameter2 ， 。 。 。）：</p>
<pre><code>#function体
</code></pre><p>对于一个真实的例子，请注意，如果在函数定义中为输入分配了一个值，这是默认值，并且仅在没有传入其他值时才使用。输入参数的顺序（在定义中）不无论如何，只要调用该函数，就会以相应的顺序输入位置参数。如果使用关键字，参数的顺序根本不重要：</p>
<p>def  string_appender （head = ‘begin’ ， tail = ‘end’ ， end_message = ‘EOL’ ）：<br>    result  =  head  +  tail  +  end_message<br>    返回 结果</p>
<blockquote>
<blockquote>
<blockquote>
<p>string_appender （’newbegin’ ， end_message  =  ‘StringOver’ ）<br>newbeginendStringOver<br>在上面的示例中，将打印函数调用的输出。head的默认值是’begin’，但是使用了’newbegin’的输入。使用了’end’尾部的默认值。并使用endmessage的输入值。请注意，必须将end_message指定为关键字参数，因为没有给出tail的值</p>
</blockquote>
</blockquote>
</blockquote>
<h4 id="类"><a href="#类" class="headerlink" title="类"></a>类</h4><p>要演示类在Python中的工作方式，请查看以下类结构。</p>
<p>类名是Pattern，它包含几个与Pattern类的任何实例（即Pattern）相关的类变量。功能是</p>
<p><strong>在里面</strong><br>函数，它创建Pattern类的实例，并使用self分配name和lengthsdict的属性。<br><strong>str</strong><br>函数定义了打印类实例时要返回的内容。<br>修剪<br>函数就像任何普通函数一样，除了所有类函数之外，self必须在输入括号中。<br>类 模式：<br>    “”， “<br>    上在SpongeRoll问题的特定图案的信息<br>    ”“”<br>    成本 =  1<br>    trimValue  =  0.04<br>    totalRollLength  =  20<br>    lenOpts  =  [ 5 ， 7 ， 9 ]</p>
<p>def  <strong>init</strong> （self ，name ，lengths  =  None ）：<br>    self 。name  =  name<br>    self 。lengthsdict  =  字典（拉链（自我。lenOpts ，长度））</p>
<p>def  <strong>str </strong> （self ）：<br>    返回 自我。名称</p>
<p>def  trim （self ）：<br>返回 模式。totalRollLength  -  总和（[ INT （我）* 自我。lengthsdict [ 我]  为 我 在 自我。lengthsdict ]）<br>这个类可以用如下：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; Pattern.cost # The class attributes can be accessed without making an instance of the class</span><br><span class="line">1</span><br><span class="line">&gt;&gt;&gt; a = Pattern(&quot;PatternA&quot;,[1,0,1])</span><br><span class="line">&gt;&gt;&gt; a.cost # a is now an instance of the Pattern class and is associated with Pattern class variables</span><br><span class="line">1</span><br><span class="line">&gt;&gt;&gt; print a # This calls the Pattern.__str__() function</span><br><span class="line">&quot;PatternA&quot;</span><br><span class="line">&gt;&gt;&gt; a.trim() # This calls the Pattern.trim() function. Note that no input is required.</span><br></pre></td></tr></table></figure></p>
<p>函数定义中的self是隐含的输入</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/03/pulp线性优化2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/08/03/pulp线性优化2/" itemprop="url">PulP线性优化（二）优化概念</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-03T16:23:51+08:00">
                2018-08-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/03/pulp线性优化2/" class="leancloud_visitors" data-flag-title="PulP线性优化（二）优化概念">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><em>本文根据PuLP文档翻译而来，原文请参考</em><br><em><a href="https://pythonhosted.org/PuLP/main/optimisation_concepts.html" target="_blank" rel="noopener">https://pythonhosted.org/PuLP/main/optimisation_concepts.html</a></em></p>
<h3 id="线性编程"><a href="#线性编程" class="headerlink" title="线性编程"></a>线性编程</h3><hr>
<p>最简单的数学程序类型是线性程序。要使您的数学程序成为线性程序，您需要满足以下条件：</p>
<ul>
<li>决策变量必须是实变量;</li>
<li>目标必须是线性表达;</li>
<li>约束必须是线性表达式。</li>
</ul>
<p>线性表达式是以下形式的表达式</p>
<script type="math/tex; mode=display">a_1x_1 + a_2x_2 + a_3x_3 + ... a_nx_n\{<=,=,>=\}b</script><p>其中$a_i$和b是已知的常数$x_i$是变量。求解线性程序的过程称为线性编程。线性编程通过修订的单纯形法（也称为原始单纯形法），双单纯形法或内点法进行。像cplex这样的解算器允许您指定使用哪种方法，但我们在此不再详述。</p>
<h3 id="整数编程"><a href="#整数编程" class="headerlink" title="整数编程"></a>整数编程</h3><hr>
<p>整数程序几乎与线性程序相同，但有一个非常重要的例外。整数程序中的某些决策变量可能只需要包含整数值。变量称为整数变量。由于大多数整数程序包含连续变量和整数变量的混合，因此它们通常称为混合整数程序。虽然线性编程的变化很小，但对解决方案过程的影响是巨大的。整数程序可能是非常难以解决的问题，并且目前有许多研究发现解决整数程序的“好”方法。可以使用分支绑定过程来解决整数程序。</p>
<p>注意对于任何合理大小的MIP，解决方案时间随着整数变量数量的增加呈指数增长。</p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/03/pulp线性优化1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/08/03/pulp线性优化1/" itemprop="url">PulP线性优化（一）优化过程</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-08-03T16:11:51+08:00">
                2018-08-03
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/算法/" itemprop="url" rel="index">
                    <span itemprop="name">算法</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/08/03/pulp线性优化1/" class="leancloud_visitors" data-flag-title="PulP线性优化（一）优化过程">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <p><em>本文根据PuLP文档翻译而来，原文请参考</em><br><em><a href="https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description" target="_blank" rel="noopener">https://pythonhosted.org/PuLP/main/the_optimisation_process.html#getting-the-problem-description</a></em></p>
<h3 id="优化过程"><a href="#优化过程" class="headerlink" title="优化过程"></a>优化过程</h3><hr>
<p>解决优化问题不是一个线性过程，但过程可分为五个一般步骤：</p>
<ul>
<li>获得问题描述 </li>
<li>制定数学公式</li>
<li>处理数学公式</li>
<li>执行一些后优化分析</li>
<li>介绍解决方案和分析</li>
</ul>
<p>但是，在此过程中通常存在“反馈循环”。例如，在制定和解决优化问题之后，您通常需要考虑解决方案的有效性（通常咨询提供问题描述的人员）。如果您的解决方案无效，您可能需要更改或更新您的配方，以包含您对实际问题的新理解。此过程显示在运筹学方法图中。<br><img src="https://pythonhosted.org/PuLP/_images/or_methodology.jpg" alt="这里写图片描述"><br>建模过程从明确定义的模型描述开始，然后使用数学方法来确定数学公式。接下来，建模者将数学公式输入到一些求解器软件中，例如Excel并求解该模型。最后，根据原始模型描述将解决方案转化为决策。</p>
<p>使用Python为您提供了建模过程的“快捷方式”。通过在Python中制定数学程序，您已经将它放入PuLP可以轻松使用的形式中，建模者可以调用许多求解器，例如CPLEX，COIN，gurobi，因此您无需将数学公式输入到求解器软件中。但是，您通常不会在配方中添加任何“硬”数字，而是使用数据文件“填充”模型，因此会涉及一些创建相应数据文件的工作。使用数据文件的优点是相同的模型可能会被不同的数据集使用多次。</p>
<h3 id="建模过程"><a href="#建模过程" class="headerlink" title="建模过程"></a>建模过程</h3><hr>
<p>建模过程是优化过程的“整洁”简化。让我们更详细地考虑优化过程的五个步骤：</p>
<h3 id="获取问题描述"><a href="#获取问题描述" class="headerlink" title="获取问题描述"></a>获取问题描述</h3><hr>
<p>此步骤的目的是提出正式，严谨的模型描述。通常，您会启动一个优化项目，其中包含问题和一些数据的抽象描述。通常，您需要花一些时间与提供问题的人（通常称为客户）交谈。通过与客户交谈并考虑可用的数据，您可以获得您习惯的更严格的模型描述。有时并非所有数据都相关，或者您需要询问客户是否可以提供其他数据。有时，可用数据的限制可能会显着改变您的模型描述和后续配方。</p>
<h3 id="制定数学公式"><a href="#制定数学公式" class="headerlink" title="制定数学公式"></a>制定数学公式</h3><hr>
<p>在此步骤中，我们从问题描述中确定关键的可量化决策，限制和目标，并在数学模型中捕获它们的依赖。我们可以将制定过程分为4个关键步骤：</p>
<ul>
<li>确定特别关注单位的决策变量（例如：我们需要确定每个进程每周运行多少小时）。</li>
<li>使用决策变量制定目标函数，我们可以构建最小化或最大化目标函数。目标函数通常反映给定决策变量值的总成本或总利润。</li>
<li>制定约束，要么是合乎逻辑的（例如，我们不能用于负数小时），要么明确指出问题描述。同样，约束以决策变量表示。</li>
<li>确定目标函数和约束所需的数据。要解决您的数学程序，您需要在目标函数和/或约束中使用一些“硬数”作为变量边界和/或变量系数。</li>
</ul>
<h3 id="解决数学程序"><a href="#解决数学程序" class="headerlink" title="解决数学程序"></a>解决数学程序</h3><hr>
<p>对于相对简单或易于理解的问题，通常可以将数学模型解决为最优性（即，识别出最佳可能的解决方案）。这是使用诸如修订的单纯形法或内点法之类的算法来完成的。然而，使用这些技术解决最优性需要很长时间的许多工业问题，因此使用不保证最优性的启发式方法来解决这些问题。</p>
<h3 id="进行一些后优化分析"><a href="#进行一些后优化分析" class="headerlink" title="进行一些后优化分析"></a>进行一些后优化分析</h3><hr>
<p>通常问题描述存在不确定性（无论是提供的数据的准确性，还是将来的数据值）。在这种情况下，我们的解决方案的稳健性可以通过执行后优化分析来检查。这涉及确定最佳解决方案在公式的各种变化下如何变化（例如，给定成本增加或特定机器失效会产生什么影响？）。这种分析对于制定战术或战略决策也很有用（例如，如果我们投资开设另一家工厂，这会对我们的收入产生什么影响？）。</p>
<p>此步骤（以及下一步）中的另一个重要考虑因素是数学程序解决方案的验证。您应该仔细考虑解决方案的变量值在原始问题描述方面的含义。确保它们对您，更重要的是您的客户有意义（这就是为什么下一步，提出解决方案和分析很重要）。</p>
<h3 id="介绍解决方案和分析"><a href="#介绍解决方案和分析" class="headerlink" title="介绍解决方案和分析"></a>介绍解决方案和分析</h3><hr>
<p>优化过程中的关键步骤是解决方案的呈现和任何后优化分析。从数学程序的解决方案转换回简明易懂的摘要与从问题描述到数学公式的转换同样重要。通过优化生成的关键观察和决策必须以易于理解的方式呈现给客户或项目利益相关者。</p>
<p>您的演示文稿是实施数学计划所产生决策的关键第一步。如果决策及其后果（通常由数学计划约束决定）没有明确而明智地呈现，那么您的最佳决策将永远不会被使用。</p>
<p>这一步也是您未来建议其他工作的机会。这可能包括：</p>
<ul>
<li>定期检查数学公式的有效性;</li>
<li>进一步分析您的解决方案，为您的客户寻找其他好处;</li>
<li>寻找未来的优化机会。</li>
</ul>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/07/30/python生成EXCEL报表乱码问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Frone Xie">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Frone's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/2018/07/30/python生成EXCEL报表乱码问题/" itemprop="url">python生成EXCEL报表乱码问题</a></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-07-30T11:21:51+08:00">
                2018-07-30
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/python/" itemprop="url" rel="index">
                    <span itemprop="name">python</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2018/07/30/python生成EXCEL报表乱码问题/" class="leancloud_visitors" data-flag-title="python生成EXCEL报表乱码问题">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数&#58;</span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        
          
            <h3 id="乱码问题"><a href="#乱码问题" class="headerlink" title="乱码问题"></a>乱码问题</h3><p>从数据库导出数据，整理后使用jaro.jaro_winkler_metric 进行文本距离的计算，计算之前需要使用decode(“utf-8”)方法进行编码转换，但是在结果数据存储成CSV是遇到了乱码问题，使用EXCEL打开就会乱码，以往都是直接使用file.open()方法进行txt文本的输出，不会出现编码问题。</p>
<p>感觉问题应该在excel端，需要加个header之类的</p>
<h3 id="解决问题"><a href="#解决问题" class="headerlink" title="解决问题"></a>解决问题</h3><p>后来在知乎找到了满意的答案。<br>使用csv写入内容，并在头部写入BOM信息 csvfile.write(codecs.BOM_UTF8) 顺利解决问题</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">#!/usr/bin/env python</span><br><span class="line"># -*- coding: UTF-8 -*-</span><br><span class="line">import csv</span><br><span class="line">import codecs</span><br><span class="line">with open(&apos;test.csv&apos;, &apos;wb&apos;) as csvfile:</span><br><span class="line">    csvfile.write(codecs.BOM_UTF8)    </span><br><span class="line">    spamwriter = csv.writer(csvfile, dialect=&apos;excel&apos;)    spamwriter.writerow([&apos;测试&apos;] * 5 + [&apos;Baked Beans&apos;])    spamwriter.writerow([&apos;Spam&apos;, &apos;Lovely Spam&apos;, &apos;Wonderful Spam&apos;])</span><br></pre></td></tr></table></figure>
<p>转自<br>作者：Nice2<br>链接：<a href="https://www.zhihu.com/question/34201726/answer/150221584" target="_blank" rel="noopener">https://www.zhihu.com/question/34201726/answer/150221584</a></p>

          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><a class="page-number" href="/page/3/">3</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Frone Xie</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">29</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">45</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/frone" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://blog.csdn.net/frone" target="_blank" title="CSDN">
                      
                        <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Frone Xie</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user">本站访客数</i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人数
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye">本站总访问量</i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>





  <script type="text/javascript">
    (function() {
      var hm = document.createElement("script");
      hm.src = "//tajs.qq.com/stats?sId=65946112";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("ohqkXUU5txOlX3MjJPCrw7jG-gzGzoHsz", "bPQhLzRSEsTqdIKAGRcHLhyN");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

  <script type="text/javascript" src="/js/src/love.js"></script><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</body>
</html>
